import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:starkylin_file_shared/src/utils/general.dart';

typedef ClientMsgCallback = void Function(MsgPack msgList);
typedef Cancel = CancelToken;

enum FileMessageType {
  file,
  dir,
  ;
}

class MessageInfo {
  final FileMessageType type;
  final String path;
  final int msgGroupId;
  final int? fileSize;
  final String? fileName;
  final String? dirName;
  int? total;
  final int? index;
  MessageInfo(
      {required this.type,
      required this.path,
      required this.msgGroupId,
      this.fileSize,
      this.dirName,
      this.total,
      this.index,
      this.fileName});

  @override
  String toString() {
    String str =
        '{"type":${type.index}, "path":"$path", "msgGroupId":$msgGroupId';
    if (fileName != null) str += ', "fileName":"$fileName"';
    if (fileSize != null) str += ', "fileSize":$fileSize';
    if (total != null) str += ', "total":$total';
    if (index != null) str += ', "index":$index';
    if (dirName != null) str += ', "dirName":"$dirName"';

    str += '}';

    return str;
  }

  static MessageInfo? fromJson(String msg) {
    log("msg $msg");
    Map<String, dynamic> map = jsonDecode(msg);
    if (map['type'] == null ||
        map['type'] is! int ||
        map['type'] >= FileMessageType.values.length) return null;

    FileMessageType type = FileMessageType.values[map['type']];
    String? path = map['path'];
    int? msgGroupId = map['msgGroupId'];
    if (path == null || msgGroupId == null) {
      log("path or msgGroupId is null");
      return null;
    }
    int? fileSize = map['fileSize'];
    String? fileName = map['fileName'];
    int? total = map['total'];
    int? index = map['index'];
    String? dirName = map['dirName'];
    return MessageInfo(
        type: type,
        msgGroupId: msgGroupId,
        path: path,
        fileSize: fileSize,
        dirName: dirName,
        total: total,
        index: index,
        fileName: fileName);
  }
}

class DownloadInfo {
  double progress = 0;
  String? speed = '0';
  int count = 0;
}

class MsgPack {
  String url = '';
  final int msgGroupId;
  final FileMessageType type;
  int fullSize = 0;
  List<String> pathList = [];
  String? dirName;

  MsgPack(
      {required this.type,
      required this.fullSize,
      required this.pathList,
      required this.msgGroupId,
      this.dirName});

  void addPaths(List<String> paths) {
    pathList.addAll(paths);
    pathList = pathList.toSet().toList();
  }

  static MsgPack? fromMsgInfo(List<MessageInfo> info) {
    if (info.isEmpty) return null;
    MsgPack pack = MsgPack(
        msgGroupId: info[0].msgGroupId,
        dirName: info[0].dirName,
        type: info[0].type,
        fullSize: info[0].fileSize ?? 0,
        pathList: [info[0].path]);
    for (var i = 1; i < info.length; i++) {
      pack.addMessageInfo(info[i]);
    }
    return pack;
  }

  void addMessageInfo(MessageInfo info) {
    pathList.add(info.path);

    if (info.fileSize != null) {
      fullSize += info.fileSize!;
    }
  }
}
